package com.salinity.kun.task.song;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;

import com.salinity.kun.helper.BaseHydrologyDataHelper;
import com.salinity.kun.helper.ConstantHelper;
import com.salinity.kun.helper.NumberHelper;
import com.salinity.kun.model.StationDateData;
import com.salinity.kun.util.DateUtil;
import com.salinity.kun.util.PathUtil;

public class DataProcess {

	public static void main(String[] args) throws ParseException, IOException {

		Path fluxPath = Paths.get("E:\\Kuner\\研究生\\南科院\\dt_flux.csv");
		Path tidePath = Paths.get("E:\\Kuner\\研究生\\南科院\\zj_input.csv");

		SimpleDateFormat sdf = ConstantHelper.getDateFormat();

		Date sDate = sdf.parse("2018/3/1  0:0");
		Date eDate = sdf.parse("2018/4/26  0:0");

		List<StationDateData> fluxData = BaseHydrologyDataHelper.getTidesFromFile(fluxPath, sDate, DateUtil.LDTime2Date(DateUtil.Date2LDTime(eDate).plusDays(1)), 1, 1, 2, 1f);
		List<StationDateData> tideData = BaseHydrologyDataHelper.getTidesFromFile(tidePath, sDate, eDate, 0, 1, 1, 1f);
		
		List<StationDateData> linearFluxData = BaseHydrologyDataHelper.doLinearInterpolation(fluxData, sDate, DateUtil.LDTime2Date(DateUtil.Date2LDTime(eDate).plusDays(1)),
				5);
//		
//		List<StationDateData> linearTideData = BaseHydrologyDataHelper.doLinearInterpolation(tideData, sDate, eDate,
//				30);

		// 除以10000
		linearFluxData.stream().forEach(new Consumer<StationDateData>() {
			@Override
			public void accept(StationDateData t) {
				t.setValue(NumberHelper.div(t.getValue(), 10000, 9));
			}
		});


//		prepareData(sDate, linearFluxData, linearTideData, fluxPath.getParent().resolve("zj2017.dat"));

		prepareData(sDate, linearFluxData, tideData, Paths.get("E:\\Kuner\\SongLaoShi\\tiaohe\\190508_ck_vs\\tiaohe\\tiaohe").resolve("zj2017_pre.dat"));

	}

	public static void prepareData(Date sDate, List<StationDateData> fluxData, List<StationDateData> tideData,
			Path outputPath) {

		SimpleDateFormat sdf = ConstantHelper.getDateFormat();
		DecimalFormat df = new DecimalFormat("#.##");
		Iterator<StationDateData> itFlux = fluxData.iterator();
		Iterator<StationDateData> itTide = tideData.iterator();
		long startTime, tempFluxTime, tempTideTime;
		double hourInterval;
		boolean isEqual = false;
		StationDateData tempFlux, tempTide;
		startTime = sDate.getTime();
		
		PathUtil.preparePath(outputPath);
		try (BufferedWriter bw = Files.newBufferedWriter(outputPath, StandardCharsets.UTF_8,
				StandardOpenOption.WRITE)) {

			// write headers
			LocalDateTime startLDTime = DateUtil.Date2LDTime(sDate);
			LocalDateTime endLDTime = DateUtil.Date2LDTime(sdf.parse(tideData.get(tideData.size() - 1).getDate()));
			StringBuffer sb = new StringBuffer();
			sb.append(startLDTime.getYear()).append(" ").append(startLDTime.getMonthValue()).append(" ")
					.append(startLDTime.getDayOfMonth()).append(" ").append(startLDTime.getHour()).append(" ")
					.append(endLDTime.getYear()).append(" ").append(endLDTime.getMonthValue()).append(" ")
					.append(endLDTime.getDayOfMonth()).append(" ").append(endLDTime.getHour()).append(" ")
					.append(tideData.size());
			bw.write(sb.toString());
			bw.newLine();
			
			while (itTide.hasNext()) {
				tempTide = itTide.next();
				tempTideTime = sdf.parse(tempTide.getDate()).getTime();
				
				while (itFlux.hasNext()) {
					tempFlux = itFlux.next();
					tempFluxTime = sdf.parse(tempFlux.getDate()).getTime();
					if (tempTideTime == tempFluxTime) {
					
						hourInterval = (tempTideTime - startTime) / (1000 * 3600f);
						// save data
						sb.delete(0, sb.length());
						sb.append(df.format(hourInterval)).append("\t").append(df.format(tempTide.getValue()))
								.append("\t").append(tempFlux.getValue());
						bw.write(sb.toString());
						bw.newLine();
						isEqual = true;
						break;
					}
				}

				if (isEqual) {
					isEqual = false;
				} else if(itTide.hasNext()){
					System.err.println("has no equal date,exit!");
					break;
				}
			}
			
			System.out.println("saved.");

		} catch (IOException | ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
